import Vue from 'vue'
import Router from 'vue-router'
import { createStore } from '@/store'
const Store = createStore()
import { routers } from './router'
//  取消路由重复点击报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)

export function createRouter() {
  const router = new Router({
    mode: 'history', // 一定要是history模式
    routes: routers
  })
  router.beforeEach((to, from, next) => {
    // 重定向url地址形式
    // if (location.pathname === '/index' || location.pathname === '/') {
    //   location.replace(location.origin + '/index.html' + location.hash)
    //   return
    // }
    // 设置loading文字
    Store.commit('common/common/SET_STATE', { loading: true, loadingText: `加载 - ${to.meta.title}` })
    // 获取高亮菜单项
    Store.commit('common/common/SET_MENU_ACTIVE', to)
    // 获取tag标签列表
    // if (window.top.location.pathname === window.location.pathname) {
    //   console.log(to)
      Store.commit('common/common/SET_TAGS_VIEWS', to)
      // 获取面包屑路径
      Store.commit('common/common/SET_B_CRUMB', to)
    // }
    next()
  })

  router.afterEach(() => {
    setTimeout(() => {
      Store.commit('common/common/SET_STATE', { loading: false, loadingText: `` })
    }, 1000)

    // window.scrollTo(0, 0)
    // next();
  })
  return router
}

